package com.lmq.backend.service.impl.user.bot;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmq.backend.mapper.BotMapper;
import com.lmq.backend.pojo.Bot;
import com.lmq.backend.pojo.User;
import com.lmq.backend.service.user.bot.BotService;
import com.lmq.backend.service.impl.utils.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.*;

@Service
public class BotServiceImpl implements BotService {

    @Autowired
    private BotMapper botMapper;

    @Override
    public Map<String, String> add(Map<String, String> data) {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();

        String title = data.get("title");
        String description = data.get("description");
        String content = data.get("content");

        Map<String, String> map = new HashMap<>();

        if(title == null || title.length() == 0) {
            map.put("message", "名称不能为空");
            return map;
        }

        if(title.length() > 100) {
            map.put("message", "名称长度大于100");
            return map;
        }

        if(description == null || description.length() > 300) {
            map.put("message", "简介长度大于300");
            return map;
        }

        if(description.length() == 0) {
            description = "这个用户很懒，什么也没有留下";
        }

        if(content == null || content.length() == 0) {
            map.put("message", "代码不能为空");
            return map;
        }

        if(content.length() > 10000) {
            map.put("message", "代码长度大于10000");
            return map;
        }

        Date now = new Date();
        Bot bot = new Bot(null, user.getId(), title, description, content, now, now);

        botMapper.insert(bot);
        map.put("message", "success");
        return map;
    }

    @Override
    public Map<String, String> remove(Map<String, String> data) {

        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
        User user = loginUser.getUser();

        Map<String, String> map = new HashMap<>();
        String idStr = data.get("id");
        if(idStr == null || idStr.length() == 0) {
            map.put("message", "Bot不存在");
            return map;
        }
        int id = Integer.parseInt(idStr);

        Bot bot = botMapper.selectById(id);

        if(bot == null) {
            map.put("message", "Bot不存在");
            return map;
        }

        if(!bot.getUserId().equals(user.getId())) {
            map.put("message", "无权限删除该Bot");
            return map;
        }

        botMapper.deleteById(id);
        map.put("message", "success");
        return map;
    }

    @Override
    public Map<String, String> update(Map<String, String> data) {

        Map<String, String> map = new HashMap<>();

        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        UserDetailsImpl loginUser = (UserDetailsImpl) usernamePasswordAuthenticationToken.getPrincipal();
        User user = loginUser.getUser();

        String idStr = data.get("id");
        if(idStr == null || idStr.length() == 0) {
            map.put("message", "Bot不存在");
            return map;
        }

        int id = Integer.parseInt(idStr);
        Bot bot = botMapper.selectById(id);

        if(bot == null) {
            map.put("message", "Bot不存在");
            return map;
        }

        if(!user.getId().equals(bot.getUserId())) {
            map.put("message", "无权限修改该Bot");
            return map;
        }

        String title = data.get("title");
        String description = data.get("description");
        String content = data.get("content");

        if(title == null || title.length() == 0) {
            map.put("message", "名称不能为空");
            return map;
        }

        if(title.length() > 100) {
            map.put("message", "名称长度大于100");
            return map;
        }

        if(description == null || description.length() > 300) {
            map.put("message", "简介长度大于300");
            return map;
        }

        if(description.length() == 0) {
            description = "这个用户很懒，什么也没有留下";
        }

        if(content == null || content.length() == 0) {
            map.put("message", "代码不能为空");
            return map;
        }

        if(content.length() > 10000) {
            map.put("message", "代码长度大于10000");
            return map;
        }

        Date date = new Date();
        Bot newBot = new Bot(bot.getId(), bot.getUserId(), title, description, content, bot.getCreateTime(), date);

        botMapper.updateById(newBot);

        map.put("message", "success");

        return map;
    }

    @Override
    public List<Bot> getList() {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();

        User user = loginUser.getUser();

        QueryWrapper<Bot> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getId());
        List<Bot> list = botMapper.selectList(wrapper);
        return list;
    }
}


